package com.kstech.ks_gaz01

/**
 * author: lenovo
 * created on: 2023/11/8 10:40
 * description:
 * Version:
 * V1.0 只生成item标签
 * V1.1 parent标签以及返回码
 * V1.2 child+group 标签
 * V1.3 添加bits 标签 内容trim去空格
 */

import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.junit.Test
import java.io.File


class CreateXML {
    @Test
    fun main() {
//        println("<table name=\"${arr[0]}\" page=\"${arr[1]}\" kind=\"${arr[2]}\"></table>")
        val file = File("src/inputExcel/Model.xlsx")
        if (file.exists()) {
            println("文件存在")
            createXmlFromExcel(file)
        } else {
            println("文件不存在${file.absolutePath}")
        }
    }


    val stringBuilder = StringBuilder()
    private fun createXmlFromExcel(file: File) {
        val workbook = WorkbookFactory.create(file)
        //检查所有sheet 以及行数，避免出现有隐藏的行数
        val maxNumSheets = workbook.numberOfSheets
        println("//===>共:${maxNumSheets}张表单")
        for (i in 0 until maxNumSheets) {
            val sheet = workbook.getSheetAt(i)
            val rownum = sheet.physicalNumberOfRows
            println("//===>第${i + 1}张,共:${rownum}行")

            logOneSheet(sheet)
        }
    }


    private fun logOneSheet(sheet: Sheet) {
        var pTmp = ""
        var cTmp = ""
        var gTmp = ""
        var reCode = ""
        var isCEnd = false
        var isGEnd = false

        stringBuilder.clear()
        val rownum = sheet.physicalNumberOfRows

        if (sheet.getRow(rownum - 1).getCell(2).toString().isNotEmpty()) {
            isCEnd = true
        }
        if (sheet.getRow(rownum - 1).getCell(3).toString().isNotEmpty()) {
            isGEnd = true
        }
        for (i in 0 until rownum) {

            stringBuilder.append("")
            val rowData = sheet.getRow(i)
            val parentName = rowData.getCell(1).toString()
            if (parentName == "一级") {
                continue
            } else if (pTmp.isEmpty()) {
                stringBuilder.append("<parent name=\"$parentName\">\n")
                pTmp = parentName
            }
//            else if (pTmp!=parentName){
//                stringBuilder.append("<parent name=\"$parentName\">\n")
//                pTmp = parentName
//            }

            val itemName = rowData.getCell(4).toString().trim()
            if (itemName.contains("参数组")) {
                val childName = rowData.getCell(2).toString().trim()
                val groupName = rowData.getCell(3).toString().trim()
                if (gTmp.isNotEmpty()&&gTmp!=groupName){
                    stringBuilder.append("</group>\n")
                }
                if (cTmp.isNotEmpty()&&cTmp!=childName){
                    stringBuilder.append("</child>\n")
                }
//                if (pTmp.isNotEmpty()&&pTmp!=parentName){
//                    stringBuilder.append("</parent>\n")
//                }

                if (cTmp.isEmpty() && childName.isNotEmpty()) {
                    stringBuilder.append("<child name=\"$childName\">\n")
                } else if (cTmp != childName) {
                    stringBuilder.append("<child name=\"$childName\">\n")
                }
                if (gTmp.isEmpty() && groupName.isNotEmpty()) {
                    stringBuilder.append("<group name=\"$groupName\">\n")
                } else if (gTmp != groupName) {
                    stringBuilder.append("<group name=\"$groupName\">\n")
                }
                gTmp = groupName
                cTmp = childName
                reCode = rowData.getCell(6).toString().trim().let {
                    it.replace(".0", "")
                }
                continue
            }


            val itemAdd = rowData.getCell(5).toString().trim().let {
                it.replace(".0", "")
            }
            val itemUnit = rowData.getCell(6).toString().trim()

            var para2 = ""
            if (reCode.isNotEmpty()) {
                para2 = reCode
                reCode = ""
            }
            /**
             * 配置bits
             */
            if (itemUnit.contains("bit")) {
                stringBuilder.append("<item para0=\"${itemAdd}\" para1=\"${itemName}\" para2=\"$para2\" para3=\"\" para4=\"\" para5=\"\">\n")
                stringBuilder.append("<bits>\n")
                val arr = itemUnit.split("\n")
                arr.forEach {

                    val index=if (it.indexOf(":")!=-1){it.indexOf(":")+1}else {it.indexOf("：")+1}
//                    println("--->${index}+$it")
//                    val itemName = it.substring(it.indexOf(":"), it.length)
                    stringBuilder.append("<bit name=\"${it.substring(index).trim()}\"/>\n")
                }

                stringBuilder.append("</bits>\n")
                stringBuilder.append("</item>\n")

            } else {
                stringBuilder.append("<item para0=\"${itemAdd}\" para1=\"${itemName}\" para2=\"$para2\" para3=\"\" para4=\"\" para5=\"$itemUnit\"/>\n")
            }

        }

        if (isGEnd) {
            stringBuilder.append("</group>\n")
        }
        if (isCEnd) {
            stringBuilder.append("</child>\n")
        }

        stringBuilder.append("</parent>\n")
        println(stringBuilder.toString())
    }

    private fun outputXml(fileName: String, stringBuilder: StringBuilder) {
        val outFileName = "src/outXML/$fileName.xml"
        val outFile = File(outFileName)
        if (!outFile.exists()) {
            outFile.createNewFile()
        }
        val content = stringBuilder.toString()

        outFile.writeText(content)
        println("Writed to file page$fileName.xml")
    }


}